package com.anyonavinfo.cpaddemo.view.musicplayerview;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.anyonavinfo.cpaddemo.Application;
import com.anyonavinfo.cpaddemo.R;
import com.anyonavinfo.cpaddemo.data.musicinfo.MusicInfo;

import java.util.List;

/**
 * Created by Administrator on 2017/2/13.
 */

public class MusicListAdapter extends RecyclerView.Adapter {


    private List<MusicInfo> musicInfoList;


    public void setMusicInfo(List<MusicInfo> musicInfo){
        this.musicInfoList = musicInfo;
    }

    public void removeItem(int position){musicInfoList.remove(position);}

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_musiclist, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new MusicListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MusicListViewHolder viewHolder = (MusicListViewHolder)holder;
        viewHolder.tv_MusicArtist.setText(musicInfoList.get(position).getArtist());
        viewHolder.tv_MusicTitle.setText(musicInfoList.get(position).getTitle() + " - ");
        viewHolder.curPosition = position;
    }

    @Override
    public int getItemCount() {
        return musicInfoList.size();
    }

    class MusicListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private Button btnDeleteMusic;
        private TextView tv_MusicTitle,tv_MusicArtist;
        private RelativeLayout rvMusicInfo;
        private int curPosition;


        public MusicListViewHolder(View itemView) {
            super(itemView);
            btnDeleteMusic = (Button) itemView.findViewById(R.id.btn_deletemusic);
            tv_MusicTitle = (TextView) itemView.findViewById(R.id.tv_music_item_title);
            tv_MusicArtist = (TextView) itemView.findViewById(R.id.tv_music_item_artist);
            rvMusicInfo = (RelativeLayout) itemView.findViewById(R.id.rv_musicinfo);

            rvMusicInfo.setOnClickListener(this);
            btnDeleteMusic.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_deletemusic:
                    onItemClick.deleteMusic(curPosition);
                    break;
                case R.id.rv_musicinfo:
                    onItemClick.playSelectedMusic(curPosition);
                    break;
            }
        }
    }

    private OnItemClick onItemClick;

    public void setOnItemClickListener(OnItemClick onItemClickListener){
        this.onItemClick = onItemClickListener;
    }

    interface OnItemClick{
        void playSelectedMusic(int selectedPosition);
        void deleteMusic(int selectedPosition);
    }
}
